package demo.excmaputil.util.excmap;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import demo.excmaputil.util.excmap.ExcMapUtil.ExcMapConfig;
import demo.excmaputil.util.excmap.ExcMapUtil.ExcMapRenderer;

/**
 * 用户对象转换Map<String,Object>配置
 * 
 * @author jishaocong
 *
 */
public class UserExcMapConfig extends ExcMapConfig {
	public static final ExcMapConfig CONFIG = new ExcMapConfig();

	private UserExcMapConfig() {
	}

	static {
		/* 渲染器 */
		Map<String, ExcMapRenderer> renderers = new HashMap<String, ExcMapRenderer>();
		ExcMapRenderer phoneRenderer = new ExcMapRenderer() {// 手机号码
			@Override
			public Object function(Object value, Object entity) {
				StringBuilder sBuilder = new StringBuilder((String) value);
				return sBuilder.replace(3, 7, "****").toString();
			}
		};
		ExcMapRenderer genderRenderer = new ExcMapRenderer() {// 性别
			@Override
			public Object function(Object value, Object entity) {
				Integer gender = (Integer) value;
				if (gender == 0) {
					return "未知";
				} else if (gender == 1) {
					return "男";
				} else if (gender == 2) {
					return "女";
				}
				return value;
			}
		};
		renderers.put("phone", phoneRenderer);
		renderers.put("gender", genderRenderer);

		/* 排除字段 */
		Set<String> excludeFields = new HashSet<String>();
		excludeFields.add("password");

		/* 日期格式 */
		Map<String, String> dateFormat = new HashMap<String, String>();
		dateFormat.put("lastLoginDate", "yyyy-MM-dd");

		/* 别名 */
		Map<String, String> aliases = new HashMap<String, String>();
		aliases.put("gender", "sex");// 将gender字段替换为别名“sex”

		/* 子配置 */
		Map<String, ExcMapConfig> subConfigs = new HashMap<String, ExcMapUtil.ExcMapConfig>();
		subConfigs.put("bankCards", BankCardExcMapConfig.CONFIG);

		CONFIG.setRenderers(renderers);
		CONFIG.setExcludeFields(excludeFields);
		CONFIG.setDateFormats(dateFormat);
		CONFIG.setSubConfigs(subConfigs);
		CONFIG.setAliases(aliases);
	}

}
